package com.baosight.risk.common.quartz;

import com.baosight.risk.api.fltd.FltdProcessService;
import com.baosight.risk.api.quartz.ISysSyncEmpleeService;
import com.baosight.risk.common.aspects.factory.AsyncFactory;
import com.baosight.risk.common.manager.AsyncManager;
import com.baosight.risk.service.quartz.entity.SyncEmpleeEntitiy;
import com.baosight.risk.service.system.entity.SysOperLog;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import com.baosight.risk.service.xtzy.impl.XtzyCompanyServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cuishihao
 * <p>
 * 定时任务
 * 同步员工
 */
@Slf4j
@Component("syncEmployeeTask")
public class SyncEmployeeTask {

    @Autowired
    private ISysSyncEmpleeService empleeService;

    @Autowired
    private XtzyCompanyServiceImpl companyService;

    @Autowired
    private FltdProcessService fltdProcessService;

    public void testParams(String params, String jobId) throws Exception {
        List<SysOperLog> logs = new ArrayList<>();
        Exception error = null;
        try {
            System.out.println("进入定时任务：同步员工中");
            List<XtzyCompany> companyList = companyService.getCompByCompType("1");
            List<SyncEmpleeEntitiy> empleeList = new ArrayList<>();
            for (XtzyCompany xtzyCompany : companyList) {
                List<SyncEmpleeEntitiy> list = empleeService.getEmpleeListFromOtherDB(xtzyCompany.getCompCode());
                if(list != null){
                    empleeList.addAll(list);
                }
            }

            Map<String, List<SyncEmpleeEntitiy>> ListHashMap = splitList(empleeList, 100);

            for (String key : ListHashMap.keySet()) {
                List<SyncEmpleeEntitiy> list = (List<SyncEmpleeEntitiy>) ListHashMap.get(key);
                empleeService.batchInsertUserList(list);
                ListHashMap.replace(key, null);
            }
            logs.add(fltdProcessService.getLog("员工同步定时任务执行成功", 0, "", jobId));
        }catch (Exception e) {
            error = e;
            logs.add(fltdProcessService.getLog("员工同步定时任务执行失败", 1, e.getMessage(), jobId));
        }
        // 保存日志到数据库
        AsyncManager.me().execute(AsyncFactory.batchRecordOper(logs));
        if (error != null) {
            throw error;
        }
    }

    /**
     * @param num  分的份数
     * @param list 需要分的集合
     */
    public Map<String, List<SyncEmpleeEntitiy>> splitList(List<SyncEmpleeEntitiy> list, Integer num) {

        int listSize = list.size();

        HashMap<String, List<SyncEmpleeEntitiy>> ListHashMap = new HashMap<String, List<SyncEmpleeEntitiy>>();

        //用于承装每个等分list
        List<SyncEmpleeEntitiy> empleeList = new ArrayList<SyncEmpleeEntitiy>();

        for (int i = 0; i < listSize; i++) {
            empleeList.add(list.get(i));
            if (((i + 1) % num == 0) || (i + 1 == listSize)) {
                ListHashMap.put("List" + i, empleeList);
                empleeList = new ArrayList<>();
            }
        }
        return ListHashMap;
    }

    public void testNoParams() {
        log.error("执行无参方法");
    }
}
